package com.zr.d_c.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zr.d_c.service.FuncService;
import com.zr.d_c.service.RoleService;
import com.zr.d_c.serviceimpl.FuncServiceImpl;
import com.zr.d_c.serviceimpl.RoleServiceImpl;
/**
 * 管理员修改用户功能
 * @author Administrator
 *
 */
public class ModifyFuncController extends HttpServlet {

	RoleService roleservice = new RoleServiceImpl();
	FuncService funcservice = new FuncServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter pw = resp.getWriter();
		String funcNames = req.getParameter("funcs");
		String rolename = req.getParameter("rolename");
		int rid = roleservice.getRoleByRname(rolename).getRid();
		String[] fnames = funcNames.split(",");
		StringBuffer func = new StringBuffer();	
		for (int i = 0; i < fnames.length; i++) {
			func.append(funcservice.getFuncByFname(fnames[i]).getFid());
			if(i!=fnames.length-1){
				func.append(",");
			}	
		}
		String funcs = func.toString();
		int flag = roleservice.modifyFuncsByRid(rid, funcs);
		if(flag > 0){
			pw.write("1");
		}else{
			pw.write("2");
		}
		pw.close();
	}
}
